package com.weetop.cfw.mine.activity

import android.os.Bundle
import android.widget.Toast
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.weetop.cfw.R
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.bean.OrderInfoBean
import com.weetop.cfw.bean.VipDataInfo
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.mine.adapter.PurchaseMembershipAdapter
import com.weetop.cfw.utils.MMKVUtils
import com.weetop.cfw.utils.PayUtils
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils
import com.xgr.easypay.callback.IPayCallback
import kotlinx.android.synthetic.main.activity_purchase_membership.*

/**
 * 会员购买界面
 */
class PurchaseMembershipActivity : CommonBaseActivity() {

    var infoid = ""

    private lateinit var purchaseMembershipAdapter: PurchaseMembershipAdapter

    override fun getLayoutId() = R.layout.activity_purchase_membership

    override fun initView(savedInstanceState: Bundle?) {
        imagePurchaseMembershipBack.setOnClickListener {
            ActivityUtils.finishActivity(this)
        }
        infoid = intent.getStringExtra("infoid")
        getVipInfo()
    }

    /**
     *  获取vip套餐信息
     */
    private fun getVipInfo() {
        RxJavaUtils.useInAppCompatActivityOnDestory(
            RetrofitUtils.getApi().getVipInfo(), this, object :
                RxJavaCallBack<VipDataInfo>() {
                override fun onSuccess(t: VipDataInfo?) {
                    if (t != null) {
                        purchaseMembershipAdapter = PurchaseMembershipAdapter(
                            R.layout.layout_item_membership_option,
                            t.data
                        )
                        membershipOptionRV.adapter = purchaseMembershipAdapter

                        purchaseMembershipAdapter.setOnItemClickListener { _, _, position ->
                            getPaySign(t.data.get(position).cfid)
                        }
                    }
                }

                override fun onErrorResponse(throwable: Throwable?) {
                    super.onErrorResponse(throwable)
                }

                override fun onRequestError(t: VipDataInfo?) {
                    super.onRequestError(t)
                }

            })
    }

    fun getPaySign(cfid: Int) {
        RxJavaUtils.useInAppCompatActivityOnDestory(
            RetrofitUtils.getApi().getVipOrder(
                MMKVUtils.getToken(),
                cfid,
                infoid
            ), this, object :
                RxJavaCallBack<OrderInfoBean>() {
                override fun onSuccess(t: OrderInfoBean?) {
                    if (t != null)
                        PayUtils.aliPay(
                            this@PurchaseMembershipActivity,
                            t.sign,
                            object : IPayCallback {
                                override fun failed(code: Int, message: String?) {
                                    ToastUtils.showShort("支付失败")
                                }

                                override fun cancel() {
                                    ToastUtils.showShort("支付取消")
                                }

                                override fun success() {
                                    ToastUtils.showShort("支付成功")
                                    setResult(88)
                                    finish()
                                }

                            })
                }

                override fun onErrorResponse(throwable: Throwable?) {
                    super.onErrorResponse(throwable)
                }

                override fun onRequestError(t: OrderInfoBean?) {
                    super.onRequestError(t)
                }

            })
    }

    override fun initData(savedInstanceState: Bundle?) {

    }
}
